package cn.sanch.org.service.impl;

import cn.sanch.basic.service.impl.BaseServiceImpl;
import cn.sanch.basic.util.MD5Utils;
import cn.sanch.basic.util.PageList;
import cn.sanch.basic.util.StrUtils;
import cn.sanch.org.domain.Employee;
import cn.sanch.org.mapper.EmployeeMapper;
import cn.sanch.org.query.DepartmentQuery;
import cn.sanch.org.query.EmployeeQuery;
import cn.sanch.org.service.IEmployeeService;
import cn.sanch.user.domain.Logininfo;
import cn.sanch.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import sun.print.PSPrinterJob;

import java.util.List;

/**
 * @author sanch37
 * 2022/5/23
 */
@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {


    @Autowired
    private LogininfoMapper logininfoMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 添加
     * @param employee
     */
    @Override
    public void save(Employee employee) {
        //员工密码添加到数据库需要加盐加密
        //获取盐值
        String salt = StrUtils.getComplexRandomString(32);
        employee.setSalt(salt);

        //调用工具类加密密码
        String md5Pwd = MD5Utils.encrypByMd5(salt + employee.getPassword());
        employee.setPassword(md5Pwd);

        //员工信息同步给logininfo
        Logininfo logininfo = employee2Logininfo(employee);
        logininfoMapper.save(logininfo);

        //获取logininfo表中的id设置给employee表中
        employee.setLogininfo_id(logininfo.getId());

        //调用父类的save方法
        super.save(employee);



        // 员工添加完成之后再添加中间表数据，因为要用到员工的role_id,判断是否添加了角色
        if (employee.getRole_id() != null) {
            // 表示给员工添加了角色
            employeeMapper.addEmployeeRole(employee.getId(), employee.getRole_id());
        }


    }

    /**
     * employee的数据同步给logininfo
     * @param employee
     * @return
     */
    private Logininfo employee2Logininfo(Employee employee) {
        //新创建一个Logininfo对象
        Logininfo logininfo = new Logininfo();

        //调用工具类同步 - 将employee中的数据同步到logininfo
        BeanUtils.copyProperties(employee, logininfo);

        //设置当前对象的type为0：管理员
        logininfo.setType(0);
        return logininfo;
    }


    /**
     * 删除
     * 员工删除了，同步删除户信息表：logininfo
     * @param id
     */
    @Override
    public void delete(Long id) {
        //1.通过id查找到员工对象
        Employee employee = employeeMapper.findOne(id);
        //2.通过员工对象获取对应的logininfo_id
        Long logininfoId = employee.getLogininfo_id();
        //3.删除户信息表
        logininfoMapper.delete(logininfoId);



        // 4.先删除中间表的数据t_employee_role，再删除员工
        employeeMapper.delEmployeeRoleByEmployeeId(id);



        super.delete(id);
    }


    /**
     * 修改
     * 员工修改了，同步修改户信息表：logininfo
     * @param employee
     */
    @Override
    public void update(Employee employee) {
        //员工密码添加到数据库需要加盐加密
        //获取盐值
        String salt = StrUtils.getComplexRandomString(32);
        employee.setSalt(salt);

        //调用工具类加密密码
        String md5Pwd = MD5Utils.encrypByMd5(salt + employee.getPassword());
        employee.setPassword(md5Pwd);

        //员工信息同步给logininfo
        Logininfo logininfo = employee2Logininfo(employee);
        //获取最新的员工对象的loginin_id
        Long logininfoId = employeeMapper.findOne(employee.getId()).getLogininfo_id();
        logininfo.setId(logininfoId);

        //更新户信息表
        logininfoMapper.update(logininfo);


        // 4.先删除中间表的数据t_employee_role，再重新添加角色
        employeeMapper.delEmployeeRoleByEmployeeId(employee.getId());
        // 员工添加完成之后再添加中间表数据，因为要用到员工的自增长id
        if (employee.getRole_id() != null) {
            // 表示给员工添加了角色
            employeeMapper.addEmployeeRole(employee.getId(), employee.getRole_id());
        }


        super.update(employee);
    }

    /**
     * 根据店铺id查询改店铺的所有店员
     * @param shopId
     * @return
     */
    @Override
    public List<Employee> list(Long shopId) {
        return employeeMapper.list(shopId);
    }
}
